/**
 * Created by DELL on 2020-04-01.
 */

creeper.CreeperConfig.token="eHk6NTU5OWI1ZmNlNzFjNDcwNzg4M2E3YTU5NDc1ZGQxODA=";
var vectorMap = new creeper.VectorMap('map123', config.wmapId, config.mapBaseUrl);

var geoJson = {
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "geometry": {
            "coordinates": [],
            "type": "LineString"
        }
    }]
};

var onClick = function (e) {
    var lngLat = e.lngLat;

    var geo1 =  [113.18096459547502, 33.77091833801974];
    var geo2 =  [113.18146323535927, 33.772241730788565];
    var geo3 =  [113.17990207537986, 33.771883352643854];
    var geo4 =  [113.18034129618525, 33.77124047556633];
    // geoJson.features[0].geometry.coordinates.push(lngLat.toArray());
    geoJson.features[0].geometry.coordinates.push(geo1);
    geoJson.features[0].geometry.coordinates.push(geo2);
    geoJson.features[0].geometry.coordinates.push(geo3);
    geoJson.features[0].geometry.coordinates.push(geo4);
    geoJson.features[0].geometry.coordinates.push(lngLat.toArray());

    vectorMap.getSource('lineSource').setData(geoJson);
};

vectorMap.on('load',function () {
    vectorMap.addSource('lineSource', {
        type: 'geojson',
        data: geoJson
    });
    vectorMap.addLayer({
        type: 'line',
        source: 'lineSource',
        id: 'lineLayer',
        paint: {
            'line-color': 'red',
            'line-width': 3
        },
        layout: {
            'line-cap': 'round',
            'line-join': 'round'
        }
    });
});

function startDrawLine() {
    geoJson.features[0].geometry.coordinates = [];

    vectorMap.on('click',onClick);
}

function finishDrawLine() {
    geoJson.features[0].geometry.coordinates = [];
    vectorMap.getSource('lineSource').setData(geoJson);

    vectorMap.off('click',onClick);
}

function get_rygj_data() {
    $.ajax({
        url:"/rygj/",
        type:"get",
        data:{'id':123},
        datatype:"json",
        success:function (data) {

            console.log(data);
            var dd = JSON.parse(data);
            var len1 =  dd.data.length;
            alert(len1);
            for(j = 0; j < len1; j++) {
                geoJson.features[0].geometry.coordinates.push([dd.data[j].lon,dd.data[j].lat]);
                // console.log(dd.data[j].lat);
                // console.log(dd.data[j].lon);
            };

            var geo1 =  [113.18096459547502, 33.77091833801974];
            var geo2 =  [113.18146323535927, 33.772241730788565];
            var geo3 =  [113.17990207537986, 33.771883352643854];
            var geo4 =  [113.18034129618525, 33.77124047556633];
            // geoJson.features[0].geometry.coordinates.push(lngLat.toArray());
//                geoJson.features[0].geometry.coordinates.push(geo1);
//                geoJson.features[0].geometry.coordinates.push(geo2);
//                geoJson.features[0].geometry.coordinates.push(geo3);
//                geoJson.features[0].geometry.coordinates.push(geo4);
            vectorMap.getSource('lineSource').setData(geoJson);
        }
    })

}